package com.auto.oa.menu.service.impl;

import java.util.List;
import java.util.Map;

import com.auto.oa.menu.dao.OaMenuDAOFactory;
import com.auto.oa.menu.domain.OaMenuInfo;
import com.auto.oa.menu.domain.OaMenuInfoExtend;
import com.auto.oa.menu.domain.OaMenuRoleMap;
import com.auto.oa.menu.service.IOaMenuInfoService;
import com.system.common.Page;
import com.system.util.BeanUtil;

public class OaMenuInfoServiceImpl implements IOaMenuInfoService {
	
	public List<OaMenuInfo> queryPageListOaMenuInfo(OaMenuInfo bean, Page page)	throws Exception {
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			param.put("displayOrder", "APME_SORT");
			List<OaMenuInfo> resultList = OaMenuDAOFactory.oaMenuInfoDAO.queryList(param, page);
			
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List<OaMenuInfoExtend> queryPageListOaMenuInfoExtend(OaMenuInfoExtend bean, Page page) throws Exception {
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			List<OaMenuInfoExtend> resultList = OaMenuDAOFactory.oaMenuInfoDAO.queryListOaMenuInfoExtend(param, page);
			
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<OaMenuInfoExtend> queryListOaMenuInfoExtend(OaMenuInfoExtend bean) throws Exception {
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			param.put("displayOrder", "A.APME_DEPTH ASC, A.APME_SORT ASC");
			List<OaMenuInfoExtend> resultList = OaMenuDAOFactory.oaMenuInfoDAO.queryListOaMenuInfoUser(param);
			
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<OaMenuInfo> queryTreeOaMenuInfo(OaMenuInfo bean) throws Exception {
		Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
		param.put("displayOrder", "APME_SORT");
		List<OaMenuInfo> resultList = OaMenuDAOFactory.oaMenuInfoDAO.queryList(param);
		
		return resultList;
	}
	
	public List<OaMenuInfo> queryListOaMenuInfo(OaMenuInfo bean) throws Exception {
		Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
		param.put("displayOrder", "APME_SORT");
		List<OaMenuInfo> resultList = OaMenuDAOFactory.oaMenuInfoDAO.queryList(param);
		
		return resultList;
	}

	public boolean saveOrUpdateOaMenuInfo(OaMenuInfo bean) throws Exception {
		try {
			if(BeanUtil.isObjectNotNull(bean)){
				if(BeanUtil.isBlank(bean.getApmeGuid())){
					//初始默认参数 树节点深度和树完整路径
					String apmeParentGuid = bean.getApmeParentGuid();
					if(!BeanUtil.isBlank(apmeParentGuid)){
						OaMenuDAOFactory.oaMenuInfoDAO.insertOaMenuInfo(bean);
						
						bean = initOaMenuInfo(bean);
						
						saveOrUpdateOaMenuInfo(bean);
					}else{
						return false;
					}
					
					return true;
				}else{
					OaMenuDAOFactory.oaMenuInfoDAO.updateOaMenuInfo(bean);
					
					return true;
				}
			}
			
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/**
	 * @说明
	 * 新增时初始化菜单信息对象
	 * 深度字段：父节点深度+1
	 * 完整路径字段：父节点路径+/+当前对象主键
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	private OaMenuInfo initOaMenuInfo(OaMenuInfo bean) throws Exception {
		String apmeGuid = bean.getApmeParentGuid();
		OaMenuInfo oaMenuInfo = getOaMenuInfo(apmeGuid);
		String apmeDepth = oaMenuInfo.getApmeDepth();
		String apmeFullPath = oaMenuInfo.getApmeFullPath();
		int newDepth = Integer.parseInt(apmeDepth) + 1;
		String newFullPath = apmeFullPath + "/" + bean.getApmeGuid();
		
		bean.setApmeDepth(String.valueOf(newDepth));
		bean.setApmeFullPath(newFullPath);
		
		return bean;
	}
	
	public boolean saveOrUpdateOaMenuRoleMap(OaMenuRoleMap bean) throws Exception {
		try {
			if(BeanUtil.isObjectNotNull(bean)){
				if(BeanUtil.isBlank(bean.getAprmmGuid())){
					//继承菜单角色配置关系
					OaMenuInfo oaMenuInfo = getOaMenuInfo(bean.getApmeGuid());
					OaMenuInfo queryOaMenuInfo = new OaMenuInfo();
					queryOaMenuInfo.setApmeFullPath(oaMenuInfo.getApmeFullPath());
					
					List<OaMenuInfo> oaMenuInfoList = queryListOaMenuInfo(queryOaMenuInfo);
					
					for (OaMenuInfo oaMenuInfoResult : oaMenuInfoList) {
						OaMenuRoleMap oaMenuRoleMap = new OaMenuRoleMap();
						oaMenuRoleMap.setAproGuid(bean.getAproGuid());
						oaMenuRoleMap.setApmeGuid(oaMenuInfoResult.getApmeGuid());
						
						OaMenuDAOFactory.oaMenuRoleMapDAO.insertOaMenuRoleMap(oaMenuRoleMap);
					}
					
					return true;
				}else{
					OaMenuDAOFactory.oaMenuRoleMapDAO.updateOaMenuRoleMap(bean);
					
					return true;
				}
			}
			
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public boolean deleteOaMenuInfo(String guid) throws Exception {
		try {
			if(!BeanUtil.isBlank(guid)){
				OaMenuDAOFactory.oaMenuInfoDAO.deleteOaMenuInfo(guid);
				
				return true;
			}
			
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public boolean deleteOaMenuRoleMap(String guid) throws Exception {
		try {
			if(!BeanUtil.isBlank(guid)){
				OaMenuDAOFactory.oaMenuRoleMapDAO.deleteOaMenuRoleMap(guid);
				
				return true;
			}
			
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public OaMenuInfo getOaMenuInfo(String guid) throws Exception {
		try {
			OaMenuInfo oaMenuInfo = null;
			if(!BeanUtil.isBlank(guid)){
				oaMenuInfo = OaMenuDAOFactory.oaMenuInfoDAO.loadOaMenuInfo(guid);
			}
			
			return oaMenuInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}
